# -*- coding: utf-8 -*-
# @Date：2024/10/5 13:24:23
# @Author：CJiaの青姝
# @FileName：issue
# @Editor：PyCharm2024
# @Remark：
from application.models import AppDetails, AppVersion
from ninja import Router, ModelSchema

from utils.cj_response import SuccessResponse, ErrorResponse

router = Router()


class AppVersionSchemaOut(ModelSchema):
    class Config:
        title = '版本对象实例'
        model = AppVersion
        model_fields = "__all__"


class AppSchemaOut(ModelSchema):
    appVersion: AppVersionSchemaOut = None

    class Config:
        title = '发布对象实例'
        model = AppDetails
        model_fields = "__all__"
        # model_exclude = ['appVersion']


@router.get('/issue/{uuid}', auth=None, response=AppSchemaOut, summary="获取应用发布信息")
def get_issue(request, uuid: int) -> SuccessResponse | ErrorResponse:
    """ 获取应用发布信息 """
    # 校验数据
    if uuid is None or uuid == '':
        return ErrorResponse(code=400, msg="应用id不能为空")
    # 获取应用
    result = AppDetails.objects.filter(id=uuid).first()
    if not result:
        return ErrorResponse(code=400, msg="应用不存在")
    if not result.appVersion and not result.versionCode:
        return ErrorResponse(code=400, msg="当前应用未发布正式版")
    # 返回结果
    return result
